/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_CharList
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-26 -- Cliff Simpkins: Initial Version
 * 2007-09-02 -- Cliff Simpkins: Added logic for storing alt characters
 * 2009-01-01 -- Cliff Simpkins: Removed public methods no longer needed
 **********************************************************************
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  public partial class WGR_CharList : PortalModuleBase {
    //List<WGR_Business.GuildEntry> _altToons;

    #region Public Properties
    public enum RosterLoadType {
      LoadUnknown = 0,
      LoadCharClass = 1,
      LoadCharProfession = 2,
      LoadCharReputation = 3,
      ShowAltChars = 4,
      LoadByLevelAll = 5,
      LoadByLevelRange = 6,
      LoadGuildRank = 7,
      LoadByGRankAll = 8,
      LoadByGRankRange = 9,
    }
    public int ParentModuleID {
      get {
        try {
          return int.Parse(lblParentModuleID.Text);
        } catch {
          return -1;
        }
      }
      set {
        lblParentModuleID.Text = value.ToString();
      }
    }
    public int ParentPortalID {
      get {
        try {
          return int.Parse(lblParentPortalID.Text);
        } catch {
          return -1;
        }
      }
      set {
        lblParentPortalID.Text = value.ToString();
      }
    }
    public bool ShowMainsOnly {
      get {
        return chbShowMainsOnly.Checked;
      }
      set {
        chbShowMainsOnly.Checked = value;
      }
    }
    public bool ArmoryColsOnly {
      get {
        return chbArmoryStyleCols.Checked;
        }
      set {
        chbArmoryStyleCols.Checked = value;
        }
      }
    public int ListSize {
      get {
        try {
          return int.Parse(lblRowsize.Text);
        } catch {
          return WGR_Business.WGRDnnConstants.SETTING_DEFAULT_LINES_PER_PAGE;
        }
      }
      set {
        lblRowsize.Text = value.ToString();
      }
    }
    public bool ShowTitle {
      get { return lblDisplayTitle.Visible; }
      set { lblDisplayTitle.Visible = value; }
    }

    public int MinLevel {
      get {
        if (lblMinLevel.Text == "") {
          return WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MINLEVEL;
        }

        try {
          return int.Parse(lblMinLevel.Text);
        } catch {
          return WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MINLEVEL;
        }
      }
      set {
        lblMinLevel.Text = value.ToString();
      }
    }
    public int MaxLevel {
      get {
        if (lblMaxLevel.Text == "") {
          return WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MAXLEVEL;
        }

        try {
          return int.Parse(lblMaxLevel.Text);
        } catch {
          return WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MAXLEVEL;
        }
      }
      set {
        lblMaxLevel.Text = value.ToString();
      }
    }

    public String DisplayTitle {
      get {
        return lblDisplayTitle.Text;
        }
      set {
        lblDisplayTitle.Text = value;
        }
      }
    public String Title {
      get {
        return lblSectionTitle.Text;
      }
      set {
        lblSectionTitle.Text = value;
        if (value.Trim() != "" && value.Trim().Length > 1) {
          imgSectionImage.ImageUrl = "images/IconLarge_" + value.Trim() + ".png";
          imgSectionImage.AlternateText = value.Trim() + " icon";
        }
        if (this.DisplayTitle.Trim() == "") {
          this.DisplayTitle = value.Trim();
          }
      }
    }

    public RosterLoadType LoadType {
      get {
        switch (lblSectionLoadType.Text) {
          case "alts":
            return RosterLoadType.ShowAltChars;
          case "class":
            return RosterLoadType.LoadCharClass;
          case "prof":
            return RosterLoadType.LoadCharProfession;
          case "rep":
            return RosterLoadType.LoadCharReputation;
          case "bylevel_all":
            return RosterLoadType.LoadByLevelAll;
          case "bylevel_range":
            return RosterLoadType.LoadByLevelRange;
          case "guild_rank":
            return RosterLoadType.LoadGuildRank;
          case "bygrank_all":
            return RosterLoadType.LoadByGRankAll;
          case "bygrank_range":
            return RosterLoadType.LoadByGRankRange;
          default:
            return RosterLoadType.LoadUnknown;
        }
      }
      set {
        switch (value) {
          case RosterLoadType.LoadCharClass:
            lblSectionLoadType.Text = "class";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadCharProfession: 
            lblSectionLoadType.Text = "prof";
            imgSectionImage.Visible = true;
            return;
          case RosterLoadType.LoadCharReputation:
            lblSectionLoadType.Text = "rep";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.ShowAltChars:
            lblSectionLoadType.Text = "alts";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadByLevelAll:
            lblSectionLoadType.Text = "bylevel_all";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadByLevelRange:
            lblSectionLoadType.Text = "bylevel_range";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadGuildRank:
            lblSectionLoadType.Text = "guild_rank";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadByGRankAll:
            lblSectionLoadType.Text = "bygrank_all";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadByGRankRange:
            lblSectionLoadType.Text = "bygrank_range";
            imgSectionImage.Visible = false;
            return;
          default:
            lblSectionLoadType.Text = "unknown";
            imgSectionImage.Visible = false;
            return;
        }
      }
    }
    public String LoadTypeString {
      get { return lblSectionLoadType.Text; }
    }

    #endregion
    #region Public Methods
    public String CharNavUrl(String charId) {
        // CRS - 2009/02/15 - Using NavigateURL instead of EditURL. DNN5.0 no longer adds in a -1 module ID (mid), which
        //                      was causing errors when users would try to follow the URL for a character.
        // URL on Globals.NavigateURL == http://www.adefwebserver.com/DotNetNukeHELP/NavigateURL/
        return Globals.NavigateURL(this.TabId, "ViewChar", "mid=" + this.ParentModuleID.ToString(), "CharID=" + charId);
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e) {
      try {
        if (DotNetNuke.Framework.AJAX.IsInstalled()) {
          DotNetNuke.Framework.AJAX.RegisterScriptManager();

          ScriptReference _overlibRef = new ScriptReference();
          _overlibRef.Path = @"~/DesktopModules/DNW-WoWGuildRoster/overlib.js";

          ServiceReference _dnwWgrServices = new ServiceReference();
          _dnwWgrServices.Path = @"~/DesktopModules/DNW-WoWGuildRoster/DNW_WGR_Services.asmx";

          ScriptManager objScriptManager = ScriptManager.GetCurrent(this.Page);
          objScriptManager.Scripts.Add(_overlibRef);
          objScriptManager.Services.Add(_dnwWgrServices);
        }


        if (!Page.IsPostBack) {
          if (this.LoadType == RosterLoadType.LoadByLevelRange ||
              this.LoadType == RosterLoadType.ShowAltChars) {
            this.ShowTitle = false;
          }

          if (!ShowTitle) {
            lblSectionSep.Visible = ShowTitle;
            imgSectionImage.Visible = ShowTitle;
          }
        }

      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }

    }

  }
}